package com.qfedu.lc.user.listener;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qfedu.lc.common.config.RabbitKeyConfig;
import com.qfedu.lc.common.config.RedisKeyConfig;
import com.qfedu.lc.common.param.GrowthAdd;
import com.qfedu.lc.common.param.MqMsg;
import com.qfedu.lc.common.sms.AliSmsUtil;
import com.qfedu.lc.user.entity.GrowthLog;
import com.qfedu.lc.user.entity.Level;
import com.qfedu.lc.user.entity.SmsLog;
import com.qfedu.lc.user.mapper.GrowthLogMapper;
import com.qfedu.lc.user.mapper.LevelMapper;
import com.qfedu.lc.user.mapper.SmsLogMapper;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.TimeUnit;

@Component
public class SmsRabbitListener {


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private SmsLogMapper smsLogMapper;

    @Autowired
    private LevelMapper levelMapper;

    @Autowired
    private GrowthLogMapper growthLogMapper;

    /**
     * 发送注册短信业务
     * 1.生成随机证码
     *
     * 2.发送阿里云短信
     *
     * 3.存入redis中验证码
     *
     * 4.手机号验证码记录日志表
     * @param msg
     */
   // 接收短信
    @RabbitListener(queues = RabbitKeyConfig.Q_SENDSMS)
    public void smsListener(MqMsg msg){

        System.out.println("msg = " + msg);


//        1.生成随机证码
          String code  = RandomUtil.randomInt(1000, 9999)+"";

//        2.发送阿里云短信
          int type = msg.getType();
          String phone = (String) msg.getMsg();
        System.out.println("向手机："+ phone +  " ----code = " + code);
          // AliSmsUtil.sendCode(phone,code) 模拟手机号发送
          if (true){

              switch (type){

                  case 1: // 注册验证码
                      // 前缀 + phone, 验证码  // 3.验证码粗存入redis
                      stringRedisTemplate.opsForValue().set(RedisKeyConfig.SMS_RCODE+phone,code,RedisKeyConfig.SMS_RCODE_TIME, TimeUnit.SECONDS);
                      // 4.记录发送验码日志
                      smsLogMapper.insert(new SmsLog(phone,"注册验证码",phone + "注册验证码"+code,new Date()));
                      break;
                  case 2:   // 登陆验证码
                      stringRedisTemplate.opsForValue().set(RedisKeyConfig.SMS_LCODE+phone,code,RedisKeyConfig.SMS_CODE_TIME, TimeUnit.SECONDS);
                      smsLogMapper.insert(new SmsLog(phone,"登录验证码",phone + "登录验证码"+code,new Date()));
                      break;
                  case 3:    // 修改密码验证码  暂时是不写

                      break;

              }

          }



    }


    /**
     * 积分队列消费者
     * @param msg
     */
    @RabbitListener(queues = RabbitKeyConfig.Q_USER_GROWTH)
    public void GrowthAddListener(MqMsg msg){

        System.out.println("msg = " + msg);
        if (msg.getType() == 5){ // 积分增加消息

            // 1.得到积分成长消息
            GrowthAdd growthAdd = (GrowthAdd) msg.getMsg();

            // 2.增加该用户积分

            //根据用户id 查询对应的积分
            QueryWrapper<Level> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id",growthAdd.getUid());

            Level level = levelMapper.selectOne(queryWrapper);
            if (level==null){ // 此时改用户没有积分  直接插入积分即可

                level = new Level(growthAdd.getUid(),1,"练气",growthAdd.getGrowth(),new Date());
                levelMapper.insert(level);
            }else { // 积分增加

                // 当前用户现有积分  需要修改级别 对应权益
                int growth = level.getGrowths() + growthAdd.getGrowth();

                //根据成长以后的积分 判断用户的等级
                if (growth<1000){
                    level.setLevel(1);
                    level.setName("练气");
                }else if (growth <100000) {
                    level.setLevel(3);
                    level.setName("金丹");
                } else if (growth <1000000) {
                    level.setLevel(4);
                    level.setName("元婴");
                } else if (growth <10000000) {
                    level.setLevel(5);
                    level.setName("化神");
                } else {
                    level.setLevel(6);
                    level.setName("仙人");
                }

                // 更新用户积分
                levelMapper.updateById(level);

            }

            // 3 记录积分变化日志
            growthLogMapper.insert(new GrowthLog(growthAdd.getUid(),growthAdd.getType(),"增加积分"+growthAdd.getGrowth(),new Date()));



        }

    }



}
